package com.example.stock.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.example.stock.comm.Cons;
import com.example.stock.comm.StrategyEnum;
import com.example.stock.comm.TranEnum;
import com.example.stock.entity.dao.AccountDao;
import com.example.stock.entity.eo.AccountEo;
import com.example.stock.entity.eo.StStockScoreEo;
import com.example.stock.entity.eo.StockTradeEo;
import com.example.stock.entity.service.impl.StStockScoreServiceImpl;
import com.example.stock.entity.service.impl.StockTradeServiceImpl;
import com.example.stock.service.IServiceAnalysis;
import com.example.stock.service.IServiceCalc;
import com.example.stock.service.IServiceTrade;
import com.example.stock.vo.DataVo;
import com.example.stock.vo.ScoreVo;
import com.example.stock.vo.common.TradeVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ServiceAnalysisImpl implements IServiceAnalysis {

    @Resource
    private StockTradeServiceImpl stockTradeServiceImpl;
    @Resource
    private StStockScoreServiceImpl stStockScoreServiceImpl;

    @Resource
    private IServiceTrade serviceTrade;
    @Resource
    private IServiceCalc serviceCale;
    @Resource
            private AccountDao accountDao;

    Logger logger = LoggerFactory.getLogger(this.getClass());


    @Override
    public void analysisBatch(Map<String, List<DataVo>> closeDataMap, StrategyEnum strategy, int m, int n, TranEnum tran, LocalDateTime startTime, int more) {
        System.out.println(m + " -> " + n);
        logger.info("m: {} n: {}", m, n);
        Map<String, List<TradeVo>> calcTradeMap = serviceCale.doMultiCalc(closeDataMap, m, n, strategy);
        Map<String, List<StockTradeEo>> doTradeMap = serviceTrade.doMultiTrade(calcTradeMap, tran,startTime,more);
        List<StockTradeEo> doTrade = serviceTrade.combineTrade(doTradeMap);
        ScoreVo score = stockTradeServiceImpl.getScore(doTrade);
        StStockScoreEo stStockScoreEo = BeanUtil.copyProperties(score, StStockScoreEo.class);
        stStockScoreEo.setM(m);
        stStockScoreEo.setN(n);
        stStockScoreEo.setTsCode(startTime.format(Cons.formatYyyyMMDD));
        stStockScoreServiceImpl.save(stStockScoreEo);
    }

    @Override
    public void analysisOne(List<DataVo> closeData,String tsCode, StrategyEnum strategy, int m, int n, TranEnum tran, LocalDateTime startTime, int more) {
        System.out.println(m + " -> " + n);
        logger.info("m: {} n: {}", m, n);
        List<TradeVo> tradeVos = serviceCale.doCalcOne(closeData, m, n, strategy);
        List<StockTradeEo> doTrade = serviceTrade.doTradeOne(tradeVos, tsCode, tran, startTime, more, 1000000);
        ScoreVo score = stockTradeServiceImpl.getScore(doTrade);
        StStockScoreEo stStockScoreEo = BeanUtil.copyProperties(score, StStockScoreEo.class);
        stStockScoreEo.setM(m);
        stStockScoreEo.setN(n);
        stStockScoreEo.setTsCode(tsCode);
        stStockScoreServiceImpl.save(stStockScoreEo);
    }

    public Double analysisUnit(Map<String, List<DataVo>> closeDataMap, StrategyEnum strategy, int m, int n, TranEnum tran, LocalDateTime startTime, int more) {
        System.out.println(m + " -> " + n);
        logger.info("m: {} n: {}", m, n);
        Map<String, List<TradeVo>> calcTradeMap = serviceCale.doMultiCalc(closeDataMap, m, n, strategy);
        Double finalMoney = serviceTrade.doMultiTradeComp(calcTradeMap, tran,startTime,more);
        return finalMoney;

    }

    @Override
    public Double analysisUnitA(Map<String, List<DataVo>> closeDataMap, StrategyEnum strategy, int m, int n, int i, TranEnum tran, LocalDateTime startTime, int more) {
        System.out.println(m + " -> " + n);
        logger.info("m: {} n: {}", m, n);
        Map<String, List<TradeVo>> calcTradeMap = serviceCale.doMultiCalc(closeDataMap, m, n,strategy);
        Double finalMoney = serviceTrade.doMultiTradeCompA(calcTradeMap, tran,startTime,more,i);
        return finalMoney;

    }

    public Double analysis00(Map<String, List<DataVo>> closeDataMap, StrategyEnum strategy, TranEnum tran, LocalDateTime startTime, int more) {
        int size = closeDataMap.size();
        Double money = 1000000D;
        Double per = money / size;
        Double total = 0D;

        for (Map.Entry<String, List<DataVo>> entry : closeDataMap.entrySet()) {
            List<DataVo> value = entry.getValue();
            List<DataVo> collect = value.stream().filter(t -> t.getTradeTime().isAfter(startTime)).collect(Collectors.toList());
            DataVo dataVo = collect.get(0);
            DataVo dataVo1 = collect.get(collect.size() - 1);
            System.out.println("FIRST: " + JSON.toJSONString(dataVo));
            System.out.println("LAST: " + JSON.toJSONString(dataVo1));
            double v = per / dataVo.getPrice() * dataVo1.getPrice();
            System.out.println("盈利: " + (v - per));
            total += v;

        }

        return total;
    }


}
